package com.cmc.cloud.cmclink.doc.service.impl.so;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoContactErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoContactConvert;
import com.cmc.cloud.cmclink.doc.entity.SoContactDO;
import com.cmc.cloud.cmclink.doc.mapper.SoContactMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoContactService;
import com.cmc.cloud.cmclink.doc.vo.sovo.socontactvo.SoContactCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.socontactvo.SoContactPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.socontactvo.SoContactRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.socontactvo.SoContactUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 订舱联系人信息 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoContactServiceImpl implements SoContactService {

    @Resource
    private SoContactMapper soContactMapper;

    @Override
    public void createSoContact(SoContactCreateReqVO createReqVO) {
        // 插入
        SoContactDO soContact = SoContactConvert.INSTANCE.convert(createReqVO);
        soContactMapper.insert(soContact);
    }

    @Override
    public void createBatch(List<SoContactCreateReqVO> createReqVOList) {
        // 插入
        List<SoContactDO> list = SoContactConvert.INSTANCE.convertBatch(createReqVOList);
        soContactMapper.insertBatch(list);
    }

    @Override
    public void createOrUpdateAndDeleteBatch(Long soId, List<SoContactUpdateReqVO> newList, List<SoContactRespVO> oldList) {
        // 无效数据直接返回
        if (CollectionUtil.isEmpty(newList) && CollectionUtil.isEmpty(oldList)) {
            return;
        }
        // 默认值
        if (CollectionUtil.isEmpty(newList)) {
            newList = new ArrayList<>();
        }
        if (CollectionUtil.isEmpty(oldList)) {
            oldList = new ArrayList<>();
        }
        List<SoContactUpdateReqVO> createList = new ArrayList<>();
        List<SoContactUpdateReqVO> updateList = new ArrayList<>();
        // 历史数据
        Set<Long> oldIds = oldList.stream().map(e -> e.getId()).collect(Collectors.toSet());
        newList.forEach(e -> {
            if (e.getId() != null) {
                if (oldIds.contains(e.getId())) {
                    // 正常更新
                    updateList.add(e);
                    oldIds.remove(e.getId()); // 剩下的数据需要删除
                } else {
                    // 数据更新非法
                    throw ServiceExceptionUtil.exception(SoContactErrorCodeConstants.SO_CONTACT_NOT_EXISTS);
                }
            } else {
                e.setSoId(soId);
                createList.add(e);
            }
        });
        if (CollectionUtil.isNotEmpty(oldIds)) {
            // 没有匹配上的就是前端删除的
            soContactMapper.deleteBatchIds(oldIds);
        }
        if (CollectionUtil.isNotEmpty(createList)) {
            List<SoContactDO> soContactCreateList = SoContactConvert.INSTANCE.convertBatchUpdate(createList);
            soContactMapper.insertBatch(soContactCreateList);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<SoContactDO> soContactUpdateList = SoContactConvert.INSTANCE.convertBatchUpdate(updateList);
            soContactMapper.updateBatch(soContactUpdateList);
        }

    }

    @Override
    public void updateSoContact(SoContactUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoContactExists(updateReqVO.getId());
        // 更新
        SoContactDO updateObj = SoContactConvert.INSTANCE.convert(updateReqVO);
        soContactMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoContact(Long id) {
        // 校验存在
        validateSoContactExists(id);
        // 删除
        soContactMapper.deleteById(id);
    }

    private void validateSoContactExists(Long id) {
        if (soContactMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoContactErrorCodeConstants.SO_CONTACT_NOT_EXISTS);
        }
    }

    @Override
    public SoContactDO getSoContact(Long id) {
        return soContactMapper.selectById(id);
    }

    @Override
    public List<SoContactDO> getSoContactList(Collection<Long> ids) {
        return soContactMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SoContactDO> getSoContactPage(SoContactPageReqVO reqVO) {
        LambdaQueryWrapperX<SoContactDO> queryWrapper = new LambdaQueryWrapperX<>();
        return soContactMapper.selectPage(reqVO, queryWrapper);
    }

    @Override
    public void deleteBySoIds(Collection<Long> sIds) {
        LambdaUpdateWrapper<SoContactDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SoContactDO::getSoId, sIds);
        soContactMapper.delete(updateWrapper);
    }

    @Override
    public List<SoContactDO> seleteBySoId(Long soId) {
        LambdaQueryWrapper<SoContactDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SoContactDO::getSoId, soId);
        queryWrapper.orderByAsc(SoContactDO::getId);
        return soContactMapper.selectList(queryWrapper);
    }
}
